﻿/*
*   学员信息页面
*/
angular.module('jisuStuHome')
.controller('stuInterlocutionController', ['FileUploader','$scope', '$rootScope', '$state', 'StuRequestTrain', 'interlocutionservice', 'sysUserInfo', 'Msg', 'T', 'LS', function (FileUploader,$scope, $rootScope, $state, ST, IS, sysUserInfo, Msg, T, LS) {

 $scope.expertConfig = ['bold', 'underline', 'italic', 'strikethrough', 'forecolor', '|', 'quote', 'fontsize', 'head', 'unorderlist', 'orderlist', 'alignleft', 'aligncenter', 'alignright', '|', 'img'];

    $scope.userInfo = sysUserInfo.getuserinfo();
    //上传文件对象
    $scope.uploader = new FileUploader();
    //初始
    $scope.init = function () {
        $scope.searchTxt = "";
        $scope.knowledgeid = "";
        //查询专栏的参数
        $scope.params = {
			userid: sysUserInfo.getuserinfo().user_ID,
            knowledgeid: "",
            orgid: sysUserInfo.getuserinfo().organization_ID,
            searchTxt: "",
            orderby: "1",
            pageIndex: 1,
            pageSize: 10,
            type: 1
        };
        //查询三条最热专栏的参数
        $scope.tjSCparams = {
            userid: sysUserInfo.getuserinfo().user_ID,
            searchTxt: "",
            knowledgeid: "",
            orgid: sysUserInfo.getuserinfo().organization_ID,
            type: 2
        };
        //查询推荐专家的参数
        $scope.tjparams = {
            userid: sysUserInfo.getuserinfo().user_ID,
            searchTxt: "",
            orgid: sysUserInfo.getuserinfo().organization_ID,
            type: 1
        };
         //查询10条最热的问答
        $scope.zrparams = {
            orgid: sysUserInfo.getuserinfo().organization_ID,
            userid:sysUserInfo.getuserinfo().user_ID,
            powerLV:sysUserInfo.getuserinfo().powerLV,
            type:"4",
        };
        $rootScope.twoparstr = "home.interlocution";
          //配置富文本编辑器的按钮
        $scope.configbtnjishiben = ['undo', 'redo', '|', 'bold', 'underline', 'italic',  'forecolor', '|','fontsize', 'head', 'unorderlist', 'orderlist', 'alignleft', 'aligncenter', 'alignright', 'img', 'insertcode'];
    }
   
    //打开问答列表
    //路由跳转
    $scope.openQuestList = function (index, item) {
        $rootScope.parstr = "home.interlocution";
        $scope.List=[];
        switch (index) {
            case 1: //跳转问答列表
                $rootScope.twoparstr = "home.interlocution.questionlist";
                LS.setLS(sysUserInfo.getuserinfo().user_ID + '_col', item);
                break;
            case 2: //我关注的专家
                $rootScope.twoparstr = "home.interlocution.myfollowexpert";
                $state.go($rootScope.twoparstr);
                break;
            case 3: //我关注的专栏
                $rootScope.twoparstr = "home.interlocution.myfollowcolnum";
                $state.go($rootScope.twoparstr);
                break
            case 4: //我收藏的问答
                $rootScope.twoparstr = "home.interlocution.collectionquestion";
                $state.go($rootScope.twoparstr);
                break
            case 5: //问答详细
                $rootScope.twoparstr = "home.interlocution.questiondetail";
                LS.setLS(sysUserInfo.getuserinfo().user_ID + '_ques', item);
                break
            case 6://专家动态
                $rootScope.twoparstr = "home.interlocution.expertdynamic";
                 LS.setLS(sysUserInfo.getuserinfo().user_ID + '_expert', item);
            default:
                break;
        }
        
    }
     //路由跳转详情页面
    $scope.openQuestList_sy = function (item) {
        $rootScope.twoparstr = "home.interlocution.questionlist";
        $rootScope.parstr = "home.interlocution";
        LS.setLS(sysUserInfo.getuserinfo().user_ID + '_col', item);
        $state.go($rootScope.twoparstr);
    }

    //*******************************专栏************************************/
    //排序筛选，默认最新
    $scope.cutDesc = function (index) {
        $scope.params.orderby = index;
        $scope.getList();
    }
    $scope.hotSCList = [];
    //查询三条最热的专栏
    $scope.getHotThree = function () {
        IS.findStuSclist($scope.tjSCparams).then(function (response) {
            if (response.errorcode == "0") {
                $scope.hotSCList = response.datas; //查询最热三条专栏
            } else {
                Msg.error("查询异常!")
            }
        }, function errorCallback() {
            console.log("连接服务器失败！");
        });
    }
    //专栏置顶
    $scope.ZDColnum=function(item){
            var state=1;//1.置顶 2.不置顶
            if(item.isTop){
                state=2;
            }
           IS.modifySpecial({specialId:item.columnId,state:state}).then(function (response) {
                if (response.errorcode == "0") {
                    item.isTop=!item.isTop;
                } else {
                    Msg.error("查询异常!")
                }
            }, function errorCallback() {
                console.log("连接服务器失败！");
            });
    }
    //查询专栏列表
    $scope.getList = function (index) {

        $scope.params.knowledgeid = $scope.knowledgeid;
        IS.findStuSclist($scope.params).then(function (response) {
            $scope.Loading = false;
            if (response.errorcode == "0") {
                $scope.List = response.datas; //专栏列表
                $scope.paginationConf.totalItems = response.numCount;
                $scope.paginationConf.currentPage = response.pageIndex;
                $scope.List.forEach(function(data,index){
                    if(data.isTop==undefined){
                        data.isTop=false;
                    }
                });
            } else {
                Msg.error("查询异常!")
            }
        }, function errorCallback() {
            $scope.Loading = false;
            console.log("连接服务器失败！");
        });
    }

    /*********************************************************************/

   

    //配置分页
    $scope.paginationConf = {
        currentPage: 1, //$location.search().currentPage ? $location.search().currentPage :  当前页码
        totalItems: 0,  // 总的记录数
        itemsPerPage: 10, // 每页显示多少条        
        perPageOptions: [10, 20],   // 每页显示记录数        
        onChange: function () {
            $scope.paginationConf.currentPage = $scope.paginationConf.currentPage == 0 ? 1 : $scope.paginationConf.currentPage;
            $scope.params.pageIndex = $scope.paginationConf.currentPage;
            $scope.params.pageSize = $scope.paginationConf.itemsPerPage;
            $scope.getList();
        }
    };
    //查询10条最热问答
    $scope.getZRList = function (index) {
        IS.findQuestionList($scope.zrparams).then(function (response) {
            $scope.Loading = false;
            if (response.errorcode == "0") {
                $scope.ZRList = response.datas;
            } else {
                Msg.error("查询异常!")
            }
        }, function errorCallback() {
            $scope.Loading = false;
            console.log("连接服务器失败！");
        });
    }
    /***********************************************统计*****************************************************/

    $rootScope.TJInfo={};
    //获取统计信息
    $scope.getTJInfo = function (index) {
        IS.findQaUserDetail({userid:sysUserInfo.getuserinfo().user_ID,username:sysUserInfo.getuserinfo().user_Name}).then(function (response) {
            $scope.Loading = false;
            if (response.errorcode == "0") {
                $rootScope.TJInfo=response.data;
                if($rootScope.TJInfo==null||$rootScope.TJInfo==undefined||$rootScope.TJInfo==""){
                    $rootScope.TJInfo.askNum=0;
                    $rootScope.TJInfo.answerNum=0;
                    $rootScope.TJInfo.adoptionNum=0;
                    $rootScope.TJInfo.followzlNum=0;
                    $rootScope.TJInfo.followzjNum=0;
                }
                if($rootScope.TJInfo.askNum<0){
                    $rootScope.TJInfo.askNum=0;
                }
                $rootScope.TJInfo.isValidate=false;//是否申请过
                $rootScope.TJInfo.ValidateState=0;//申请过的状态码

            } else {
                Msg.error("查询异常!")
            }
        }, function errorCallback() {
            $scope.Loading = false;
            console.log("连接服务器失败！");
        });
    }

     //新业务开始,专家可以修改自己的 信息**************************************************************************************************************

    //表单显示
    $scope.showAddForm = false;

    //打开or关闭
    $scope.closeForm = function () {
        $scope.showAddForm = !$scope.showAddForm;
    }
    //单个事件
    $scope.achievements = {
        title: "", //标题
        event: ""//事件
    };
    //删除事件
    $scope.delEvent = function (id) {
        $scope.EditItem.achievements.splice(id, 1);
    }
    $scope.addEvent = function () {
        if ($scope.achievements.year == undefined || $scope.achievements.year.length <= 0) {
            Msg.warning("请输入事件年份！");
            return;
        }
        if ($scope.achievements.month == undefined || $scope.achievements.month.length <= 0) {
            Msg.warning("请输入事件月份！");
            return;
        }
        if ($scope.achievements.title == undefined || $scope.achievements.title.length <= 0) {
            Msg.warning("请输入事件标题！");
            return;
        }
        if ($scope.achievements.event == undefined || $scope.achievements.event.length <= 0) {
            Msg.warning("请输入事件详情！");
            return;
        }
        $scope.EditItem.achievements.push($scope.achievements);
        $scope.achievements = {
            title: "", //标题
            event: ""//事件
        };
    }
    $scope.EditItem={};
    //打开新增、编辑窗口
    $scope.openForm = function () {
            $scope.EditItem.userid = sysUserInfo.getuserinfo().user_ID,
            $scope.EditItem.statusId = ""; //身份id
            $scope.EditItem.achievements = []//事件,保存的时候换成string
            $scope.getShInfo();
            $scope.getAchievement();
            $scope.closeForm();
    }
    //保存的loading
    $scope.isBtnLoading = false;
    //新增
    $scope.addinfo = function () {
        if ($scope.EditItem.expertDesc.length <= 0) {
            Msg.warning("请输入专家描述！");
            return;
        }
        if ($scope.EditItem.autograph.length <= 0) {
            Msg.warning("请输入专家签名！");
            return;
        }
        if ($scope.EditItem.title.length <= 0) {
            Msg.warning("请输入专家头衔！");
            return;
        }
        if ($scope.EditItem.city.length <= 0) {
            Msg.warning("请输入专家所在城市！");
            return;
        }
        if ($scope.EditItem.address.length <= 0) {
            Msg.warning("请输入专家所在地址！");
            return;
        }
        if ($scope.EditItem.excellentField.length <= 0) {
            Msg.warning("请输入擅长领域！");
            return;
        }
        delete $scope.EditItem.isToolShow;
        delete $scope.EditItem.ccList;
        delete $scope.EditItem.createDate;
        delete $scope.EditItem.updateDate;
        $scope.saveExpert();
    }
    
    //保存专家
    $scope.saveExpert = function () {
        $scope.isBtnLoading = true;
        $scope.EditItem.achievements = JSON.stringify($scope.EditItem.achievements);
        IS.saveExpert($scope.EditItem).then(function (response) {
            $scope.isBtnLoading = false;
            if (response.errorcode == "0") {
                Msg.success("保存成功！");
                $scope.closeForm();
            } else if (response.errorcode == "29") {
                Msg.warning("该专家已存在！");
            } else if (response.errorcode == "27") {
                Msg.warning("该专家不存在！");
            } else {
                Msg.error("错误", "保存异常!");
                $scope.EditItem.achievements = JSON.parse($scope.EditItem.achievements);
            }
        }, function () {
            $scope.isBtnLoading = false;
            $scope.EditItem.achievements = JSON.parse($scope.EditItem.achievements);
        });
    }

    //获取专家的身份信息
    $scope.getShInfo = function () {
        IS.findExpertStatus({ expertId: $scope.EditItem.expertId, pageIndex: 1, pageSize: 1 }).then(function (response) {
            if (response.errorcode == "0") {
                if (response.datas != undefined && response.datas.length > 0&& response.datas[0]!=null) {
                    $scope.EditItem.statusId = response.datas[0].statusId;
                }
            } else {
                Msg.error("错误", "获取异常!");
            }
        });
    }
    //获取专家的事件列表
    $scope.getAchievement = function () {
        IS.findAchievement({ expertId: $scope.EditItem.expertId }).then(function (response) {
            if (response.errorcode == "0") {
                if (response.datas != undefined && response.datas.length > 0) {
                    $scope.EditItem.achievements = response.datas;
                }
            } else {
                Msg.error("错误", "获取异常!");
            }
        });
    }
    /***********************************************初始*****************************************************/
    //初始
    $scope.init();
    //查询专栏列表
    $scope.getList();

    //查询三条最热专栏
    $scope.getHotThree();
     //查询10条最热问答
    $scope.getZRList();
    //获取统计信息
    $scope.getTJInfo();
    /********************************************新增***************************************************/
    $scope.showForm = false;

    // 关闭弹框
    $scope.closeOrOpenForm = function () {
        $scope.showForm = !$scope.showForm;
    }
    /******************************************查询知识分类**********************************************/
    $scope.knowParam = {
        userId: sysUserInfo.getuserinfo().user_ID,
        powerLV: "2",
        knowledge_Id: "", //父级id
        orgId: sysUserInfo.getuserinfo().organization_ID
    };
    // 获取知识结构列表
    $scope.getKnowledgeList = function () {
        ST.findKnowledgeList($scope.knowParam).then(function (response) {
            if (response.errorcode == "0") {// 成功         
                $scope.knowList = response.datas;
            } else {
                Msg.error("分类获取失败！");
            }
        }, function errorCallback() {
            Msg.error("连接服务器失败！");
        });
    }
    $scope.getKnowledgeList();
    $scope.clickList = [];
    //选择知识分类
    $scope.clickKnow = function (item) {

        $scope.clickList.push(item);
        if (item == undefined) {
            $scope.knowParam.knowledge_Id = "";
            $scope.clickList = [];
            $scope.knowledgeid = "";
        } else {
            item.sortindex = $scope.clickList.length;
            $scope.knowParam.knowledge_Id = item.knowledge_Id;
            $scope.knowledgeid = item.knowledge_Id;
        }

        $scope.getKnowledgeList();
        $scope.getList();
    }
    //选择其中一个
    $scope.backKnow = function (item) {
        if (item == undefined) {
            $scope.knowParam.knowledge_Id = "";
            $scope.clickList = [];
            $scope.knowledgeid = "";
        } else {
            $scope.knowParam.knowledge_Id = item.knowledge_Id;
            $scope.clickList.forEach(function (data, index) {
                if (index >= item.sortindex) {
                    $scope.clickList.splice(index, 1);
                }
            });
            $scope.knowledgeid = item.knowledge_Id;
        }

        $scope.getKnowledgeList();
        $scope.getList();
    }
    /***********************************************成为专家************************************************/
    $scope.showExpert = false;
    $scope.closeExpertForm = function () {
        $scope.showExpert = !$scope.showExpert;
    }
} ]);




